<template>
  <view class="goods_list">
    <view v-for="good in goodsList" :key='good.goods_id' @click="gotoDetail(good.goods_id)">
      <myGoodItem :img='good.goods_small_logo' :title="good.goods_name" :price="good.goods_price"></myGoodItem>
    </view>

  </view>
</template>

<script>
  import {
    reqGoodsList
  } from '../../api/api.js';
  export default {
    onLoad(options) {
      this.queryObj.query = options.query || '';
      this.queryObj.cid = options.cid || '';
      this.getGoodsList();
    },
    data() {
      return {
        goodsList: [],
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页的数据量
          pagesize: 10,
        },
        total: 0,
        isLoading:false
      };
    },
    onReachBottom() {
      if (this.queryObj.pagenum <= Math.ceil(this.total / this.queryObj.pagesize)-1) {
        this.getGoodsList();
      }
    },
    onPullDownRefresh(){
      this.queryObj.pagenum=1;
      this.total=0;
      this.isLoading=false;
      this.goodsList=[];
      this.getGoodsList(true);
    },
    methods: {
      gotoDetail(id){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+id
        })
      },
      async getGoodsList(isFreshing) {
        if(!this.isLoading){
          this.isLoading=true;
          let res = await reqGoodsList(this.queryObj);
          isFreshing&&uni.stopPullDownRefresh();
          this.isLoading=false;
          if (res.data.meta.status === 200) {
            this.goodsList = [...this.goodsList, ...res.data.message.goods];
            this.total = res.data.message.total;
            this.queryObj.pagenum++;
          } else {
            return uni.$showMsg();
          }
        }
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
